package kcg;

import static org.junit.Assert.assertTrue;
import kcg.test.RobotTestBed;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.events.BattleStartedEvent;
import robocode.control.snapshot.IRobotSnapshot;

/**
 * Illustrates JUnit testing of Robocode robots. This test simply verifies that MikeTerry
 * path works.
 * 
 * @author Philip Johnson Modified for personal use by Kelvin Green
 */

public class TestMikeTerryPath extends RobotTestBed {

  private double height = 0;
  private double width = 0;
  private boolean leftWall = false;
  private boolean rightWall = false;
  private boolean topWall = false;
  private boolean bottomWall = false;
  int first = 0;
  int next = 0;
  int last = 0;

  @Override
  public String getRobotNames() {
    return "sample.Fire,kcg.MikeTerry";
  }

  /**
   * This test runs for five rounds.
   * 
   * @return The number of rounds in this test
   */
  @Override
  public int getNumRounds() {
    return 5;
  }

  /**.
   * After each turn, check to see if MikeTerry is turning clockwise during battle.
   * 
   * @param event Information about the current state of the battle
   */

  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
    double x = robot.getX();
    double y = robot.getY();

    // Robot is close to the right wall.
    if (x > (width - 40)) {
      if (!rightWall) {
        if (first == 0) {
          first = 1;
          last = 4;
          next = 2;
        }
        else if (next == 1) {
          if (!(rightWall & leftWall & topWall & bottomWall)) {
            next++;
          }
        }

      }
      rightWall = true;
    }
    // Robot is close to the left wall.
    else if (x < 40) {
      if (!leftWall) {
        if (first == 0) {
          first = 2;
          last = 1;
          next = 3;
        }
        else if (next == 2) {
          if (!(rightWall & leftWall & topWall & bottomWall)) {
            next++;
          }
        }
      }
      leftWall = true;
    }
    // Robot is close to the bottom wall.
    else if (y > (height - 40)) {
      if (!bottomWall) {

        if (first == 0) {
          first = 1;
          last = 2;
          next = 3;
        }
        else if (next == 3) {
          if (!(rightWall & leftWall & topWall & bottomWall)) {
            next++;
          }
        }
      }
      bottomWall = true;
    }
    // Robot is close to the top wall.
    else if (y < 40) {
      if (!topWall) {

        if (first == 0) {
          first = 1;
          last = 3;
          next = 1;
        }
        else if (next == 4) {
          if (!(rightWall & leftWall & topWall & bottomWall)) {
            next = 1;
          }
        }
      }
      topWall = true;
    }

  }

  /**
   * Asserts that MikeTerry is turning clockwise after each round.
   * 
   * @param event Details about the started battle
   */

  @Override
  public void onBattleStarted(BattleStartedEvent event) {
    height = event.getBattleRules().getBattlefieldHeight();
    width = event.getBattleRules().getBattlefieldWidth();
  }

  /**
   * Asserts that MikeTerry is turning clockwise after each round.
   * 
   * @param event Details about the completed battle
   */
  @Override
  public void onBattleCompleted(BattleCompletedEvent event) {
    if (rightWall & leftWall & topWall & bottomWall) {
      assertTrue(last == next);
    }
  }

}